package com.zhihuichongcheng.application.ui.im.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.GsonUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.base.CustomWebviewActivity;
import com.zhihuichongcheng.application.bean.im.NoticeBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.im.adapter.NoticeAdapter;
import com.zhihuichongcheng.application.ui.main.activity.MainActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 3water on 2016/10/28.
 */

public class NoticeActivity extends BaseActivity {
    private XRecyclerView mRcvNotice;
    private List<NoticeBean> mListData = new ArrayList<>();
    private NoticeAdapter mAdapter = null;
    private String mTitle;
    private boolean mIsFromPush;
    private int index;
    private LinearLayout mLlNoData;
    private int REQUEST_WEBVIEW = 1;
    private int mPageSize = 10;
    private int mPage = 1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);
        initView();
        initData();
    }

    private void initView() {
        setTitle("通知公告");
        setArrowTitle("消息");
        mLlNoData = (LinearLayout) findViewById(R.id.ll_nodata);
        mRcvNotice = (XRecyclerView) findViewById(R.id.rcv_notice);
        mAdapter = new NoticeAdapter(mContext);
        mRcvNotice.setLayoutManager(new LinearLayoutManager(mContext));
        mRcvNotice.setAdapter(mAdapter);
        mRcvNotice.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPage = 1;
                loadData(mPage);

            }

            @Override
            public void onLoadMore() {
                mPage++;
                loadData(mPage);
            }
        });
        mAdapter.setOnItemCLick(new OnItemOnclick<NoticeBean>() {
            @Override
            public void onClick(NoticeBean noticeBean, int position) {
                index = position;
                Intent intent = new Intent(mContext, CustomWebviewActivity.class);
                intent.putExtra("title", noticeBean.getTitle());

                if (mTitle.equals("会议通知")) {
                    String url = "https://web.jm-ccwy.com/#/message-detail?id=";
                    intent.putExtra("url", url + noticeBean.getMeetingAnnouncementId() + "&type=3");
                    startActivityForResult(intent, REQUEST_WEBVIEW);
                    HashMap<String, String> params = new HashMap<String, String>();
                    params.put("token", mUser.getToken());
                    params.put("meetingAnnouncementId", noticeBean.getMeetingAnnouncementId());
                    mManager.post(NetworkAPI.MettingNoticeLog, new HttpCallback() {
                        @Override
                        public void onSuccess(int code, String value, String msg) {

                        }

                        @Override
                        public void onFailure(String value) {

                        }
                    }, params);
                } else {
                    String url = "https://web.jm-ccwy.com/#/message-detail?id=";
                    intent.putExtra("url", url + noticeBean.getAnnouncementId() + "&type=2");
                    startActivityForResult(intent, REQUEST_WEBVIEW);
                    HashMap<String, String> params = new HashMap<String, String>();
                    params.put("token", mUser.getToken());
                    params.put("announcementId", noticeBean.getAnnouncementId());
                    mManager.post(NetworkAPI.AnnouncementNoticeLog, new HttpCallback() {
                        @Override
                        public void onSuccess(int code, String value, String msg) {

                        }

                        @Override
                        public void onFailure(String value) {

                        }
                    }, params);
                }

            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("title")) {
            mTitle = intent.getStringExtra("title");
        }
        if (intent != null) {
            mIsFromPush = intent.getBooleanExtra("isFromPush", false);
        }
        setTitle(mTitle);
        loadData(mPage);
        DialogUtil.showLoadDiadlog(mContext);
    }

    private void loadData(int page) {
        HashMap<String, String> params = new HashMap<>();
        params.put("token", mUser.getToken());
        params.put("pageRows", "" + mPageSize);
        params.put("curPage", "" + page);
        if (mTitle.equals("会议通知")) {
            spUtil.putBoolean(mContext, "mettingNotice", false);
            mManager.post(NetworkAPI.MettingNoticeList, new HttpCallback() {
                @Override
                public void onSuccess(int code, String value, String msg) {
                    DialogUtil.dismiss(mContext);
                    if (code == 1) {
                        List<NoticeBean> list = GsonUtil.paraeFromStringToList(value, NoticeBean.class);
                        if (mPage == 1) {
                            mAdapter.clear();
                            if (list.size() == 0) {
                                mLlNoData.setVisibility(View.VISIBLE);
                                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLlNoData.getLayoutParams();
                                params.setMargins(0, (DeviceUtil.getHeight(mContext) - DeviceUtil.dip2px(mContext, 50)) / 10 * 4 - DeviceUtil.dip2px(mContext, 75), 0, 0);
                                mLlNoData.setLayoutParams(params);
                                mRcvNotice.setVisibility(View.GONE);
                            } else {
                                mLlNoData.setVisibility(View.GONE);
                                mRcvNotice.setVisibility(View.VISIBLE);
                            }
                        }
                        mAdapter.setData(list);
                        mAdapter.notifyDataSetChanged();
                        mRcvNotice.loadMoreComplete();
                        mRcvNotice.refreshComplete();
                        if (list.size() < mPageSize) {
                            mRcvNotice.setNoMore(true);
                        }
                    } else {

                    }
                }

                @Override
                public void onFailure(String value) {
                    DialogUtil.dismiss(mContext);
                    mLlNoData.setVisibility(View.VISIBLE);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLlNoData.getLayoutParams();
                    params.setMargins(0, (DeviceUtil.getHeight(mContext) - DeviceUtil.dip2px(mContext, 50)) / 10 * 4 - DeviceUtil.dip2px(mContext, 75), 0, 0);
                    mLlNoData.setLayoutParams(params);
                    mRcvNotice.setVisibility(View.GONE);
                }
            }, params);
        } else {
            spUtil.putBoolean(mContext, "notice", false);
            mManager.post(NetworkAPI.AnnouncementNoticeList, new HttpCallback() {
                @Override
                public void onSuccess(int code, String value, String msg) {
                    DialogUtil.dismiss(mContext);
                    if (code == 1) {
                        List<NoticeBean> list = GsonUtil.paraeFromStringToList(value, NoticeBean.class);
                        if (mPage == 1) {
                            mAdapter.clear();
                            if (list.size() == 0) {
                                mLlNoData.setVisibility(View.VISIBLE);
                                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLlNoData.getLayoutParams();
                                params.setMargins(0, (DeviceUtil.getHeight(mContext) - DeviceUtil.dip2px(mContext, 50)) / 10 * 4 - DeviceUtil.dip2px(mContext, 75), 0, 0);
                                mLlNoData.setLayoutParams(params);
                                mRcvNotice.setVisibility(View.GONE);
                            } else {
                                mLlNoData.setVisibility(View.GONE);
                                mRcvNotice.setVisibility(View.VISIBLE);
                            }
                        }
                        mAdapter.setData(list);
                        mAdapter.notifyDataSetChanged();
                        mRcvNotice.loadMoreComplete();
                        mRcvNotice.refreshComplete();
                        if (list.size() < mPageSize) {
                            mRcvNotice.setNoMore(true);
                        }
                    } else {

                    }
                }

                @Override
                public void onFailure(String value) {
                    DialogUtil.dismiss(mContext);
                    mLlNoData.setVisibility(View.VISIBLE);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLlNoData.getLayoutParams();
                    params.setMargins(0, (DeviceUtil.getHeight(mContext) - DeviceUtil.dip2px(mContext, 50)) / 10 * 4 - DeviceUtil.dip2px(mContext, 75), 0, 0);
                    mLlNoData.setLayoutParams(params);
                    mRcvNotice.setVisibility(View.GONE);
                }
            }, params);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
//        ToastOpt.createToast(mContext,"11111");
        if (mIsFromPush) {
            onBaClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mIsFromPush) {
                        startActivity(MainActivity.class, null);
                    }
                    finish();
                }
            });
        }
        mAdapter.setDataRead(index);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mIsFromPush) {
            startActivity(MainActivity.class, null);
        }
        finish();
        return super.onKeyDown(keyCode, event);
    }
}
